<?php
/**
 * 图像处理类
 * 
 * @version $Id: Image.php 174 2011-05-18 02:38:13Z linsir123 $
 */
class comImage
{
	/**
	 * @var 创建图像函数列表
	 */
	static $ext2Create = array('png'=>'imagecreatefrompng', 'gif'=>'imagecreatefromgif', 'jpg'=>'imagecreatefromjpeg', 'jpeg'=>'imagecreatefromjpeg');
	
	/**
	 * @var 展示图像函数列表
	 */
	static $ext2Display = array('png'=>'imagepng', 'gif'=>'imagegif', 'jpg'=>'imagejpeg', 'jpeg'=>'imagejpeg');
	
	/**
	 * @var 展示图像MIME列表
	 */
	static $ext2Mime = array('png'=>'image/png','gif'=>'image/gif','jpg'=>'image/jpeg','jpeg'=>'image/jpeg');
	
	///
	public $imageWidth;
	public $imageHeight;
	public $saveDir;
	public $fileExt;
	public $im;
	
	/**
     * 构造函数
     */
	public function __construct($file, $fileExt, $saveDir = null)
	{
		if ($saveDir) {
			$saveDir = str_replace(array('/', '\\'), DS, $saveDir);
			$this->saveDir = ((substr($saveDir, -1) == DS) ? substr($saveDir, 0, -1) : $saveDir).DS;
		}
		
		///
		$this->fileExt = strtolower($fileExt);
		if (isset(self::$ext2Create[$this->fileExt]) && file_exists($file)) {
			$im = call_user_func_array(self::$ext2Create[$this->fileExt], array($file));
			$this->_setIm($im);
			
			if ($this->fileExt == 'png') {
				// PNG 透明处理(创建透明填充色)
				imagealphablending($this->im, true);
				imagesavealpha($this->im, true);
			}
		}
	}
	
	/**
	 * 设置图像尺寸
	 */
	private function _setIm($im = null)
	{
		if ($im) {
			$this->im = $im;
			$this->imageWidth = imagesx($this->im);
			$this->imageHeight = imagesy($this->im);
		}
	}
	
	/**
	 * 算出水印位置
	 */
	private function _waterPostion($postion, $logoWidth, $logoHeight)
	{
		$x = $y = 0;
		$imageWidth = $this->imageWidth;
		$imageHeight = $this->imageHeight;
		switch ($postion) {
			case 1:
				$x = +5;
				$y = +5;
				break;
			case 2:
				$x = ($imageWidth - $logoWidth) / 2;
				$y = +5;
				break;
			case 3:
				$x = $imageWidth - $logoWidth - 5;
				$y = +5;
				break;
			case 4:
				$x = +5;
				$y = ($imageHeight - $logoHeight) / 2;
				break;
			case 5:
				$x = ($imageWidth - $logoWidth) / 2;
				$y = ($imageHeight - $logoHeight) / 2;
				break;
			case 6:
				$x = $imageWidth - $logoWidth;
				$y = ($imageHeight - $logoHeight) / 2;
				break;
			case 7:
				$x = +5;
				$y = $imageWidth - $logoHeight - 5;
				break;
			case 8:
				$x = ($imageWidth - $logoWidth) / 2;
				$y = $imageHeight - $logoHeight - 5;
				break;
			case 9:
				$x = $imageWidth - $logoWidth - 5;
				$y = $imageHeight - $logoHeight - 5;
				break;
		}
		
		return array($x, $y);
	}
	
	/**
	 * 水印文本
	 */
	private function _waterText($postion, $params = array())
	{
		$default = array('text'=>'lxsphp','size'=>40,'angle'=>0,'alpha'=>80,'color'=>'#000ccc');
		list($text, $size, $angle, $alpha, $color) = array_values(array_merge($default, $params));
		if (empty($text)) 
			return false;
		
		///
		$colorRGB = lpString::hex2rgb($color);
		$fontPath = dirname(__FILE__).DS.'fonts/verdanab.ttf';
		if ($size < 0 || $size > 100) $size = 40;
		if ($alpha < 0 || $alpha > 100) $alpha = 80;
		
		///
		$box = imagettfbbox($size, $angle, $fontPath, $text);
		$logoHeight = max($box[1], $box[3]) - min($box[5], $box[7]);
		$logoWidth = max($box[2], $box[4]) - min($box[0], $box[6]);
		$ax = min($box[0], $box[6]) * -1;
		$ay = min($box[5], $box[7]) * -1;
		
		///
		list($x, $y) = $this->_waterPostion($postion, $logoWidth, $logoHeight);
		$color = imagecolorallocatealpha($this->im, $colorRGB[0], $colorRGB[1], $colorRGB[2], $alpha);
		return imagettftext($this->im, $size, $angle, $x + $ax, $y + $ay, $color, $fontPath, $text);
	}
	
	/**
	 * 水印图片
	 */
	private function _waterImage($postion, $logoFile = null)
	{
		$ext2Create = self::$ext2Create[pathinfo($logoFile, PATHINFO_EXTENSION)];
		if (empty($ext2Create) || !file_exists($logoFile) || !($watermarkLogo = $ext2Create($logoFile))) 
			return false;
			
		///
		$logoWidth = imagesx($watermarkLogo);
		$logoHeight = imagesy($watermarkLogo);
		
		///
		list($x, $y) = $this->_waterPostion($postion, $logoWidth, $logoHeight);
		$params = array($this->im, $watermarkLogo, $x, $y, 0, 0, $logoWidth, $logoHeight);
		if ($ext2Create == 'imagecreatefrompng') 
			$function = 'imagecopy';
		else {
			imagealphablending($this->im, true);
			$function = 'imagecopymerge';
			$params[] = 65;
		}
		
		return call_user_func_array($function, $params);
	}
	
	/**
	 * 添加水印
	 */
	public function water($type = 1, $postion = 5)
	{
		if (empty($this->im)) 
			return false;
			
		///
		$args = func_get_args();
		switch ($type) {
			case 1:
			default:
				$params = isset($args[2]) && is_array($args[2]) ? $args[2] : array();
				$this->_waterText($postion, $params);
				break;
				
			case 2:
				$logoFile = isset($args[2]) ? $args[2] : null;
				$this->_waterImage($postion, $args[2]);
				break;
		}
	}
    
	/**
	 * 保持图像长宽比的情况下将图像裁到指定大小
	 */
	public function thumb($width, $height = 0)
	{
		if (empty($this->im)) 
			return false;
			
		///
		$imageWidth = $this->imageWidth;
		$imageHeight = $this->imageHeight;
		if (empty($width) || empty($height)) {
			
			// 按一边进行缩放
			$width or ($width = $imageWidth);
			$height or ($height = $imageHeight);			
			if ($width >= $imageWidth && $height >= $imageHeight)
				return false;
			
			if ($width < $imageWidth)
				$height = intval(($width / $imageWidth) * $imageHeight);
			elseif ($height < $imageHeight)
				$width = intval(($height / $imageHeight) * $imageWidth);
				
		} else {
			
			// 限定于指定的宽与高内
			if ($width >= $imageWidth && $height >= $imageHeight)
				return false;
				
			if ($width > $height) {
				$height_scale = intval(($width / $imageWidth) * $imageHeight);
				if ($height_scale > $height) 
					$width = intval(($height / $height_scale) * $width);
				else 
					$height = $height_scale;
			} else {
				$width_scale = intval(($height / $imageHeight) * $imageWidth);
				if ($width_scale > $width) 
					$height = intval(($width / $width_scale) * $height);
				else 
					$width = $width_scale;
			}
		}
		
		$im = imagecreatetruecolor($width, $height);
		if ($this->fileExt == 'png') {
			// PNG 透明处理(创建透明填充色)
			imagealphablending($im, true);
			imagesavealpha($im, true);
			$bgcolor = imagecolorallocatealpha($im, 0, 0, 0, 127);
			imagefill($im, 0, 0, $bgcolor);
		}
			
		$params = array($im, $this->im, 0, 0, 0, 0, $width, $height, $imageWidth, $imageHeight);
		call_user_func_array('imagecopyresampled', $params);
		imagedestroy($this->im);
		
		$this->_setIm($im);
		return true;
	}
	
	/**
	 * 保存图像
	 */
	public function save($fileName)
	{
		$fileName = $this->saveDir.$fileName.'.'.$this->fileExt;
		if ($this->im && isset(self::$ext2Display[$this->fileExt])) 
			return self::$ext2Display[$this->fileExt]($this->im, $fileName);
		
		return false;
	}
	
	/**
	 * 显示图像
	 */
	public function display()
	{
		header("Content-type:".self::$ext2Mime[$this->fileExt]);
		if ($this->im && isset(self::$ext2Display[$this->fileExt])) 
			call_user_func_array(self::$ext2Display[$this->fileExt], array($this->im));
	}
}
